package models;

import java.util.Collections;
import java.util.List;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;

import entities.IpAddress;

public class IpAddressModel {
	private Objectify ofy;
	
	private Cache cache = null;
	
	private String prefix = "ip_address_";
	
	private int limit = 100;
	
	private static IpAddressModel instance = null;
	
	public static IpAddressModel getInstance() {
		if (instance == null) {
			instance = new IpAddressModel();
		}
		
		return instance;
	}
	
	private IpAddressModel() {
		try {
			ObjectifyService.register(IpAddress.class);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		/*try {
            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        } catch (CacheException e) {
           //e.printStackTrace();
        }*/
		
        ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
		ofy = ObjectifyService.begin(opts);
	}
	
	public void insert(IpAddress ipAddress) {
		if (cache != null) {
			 cache.put(prefix + ipAddress.getIp(), ipAddress);
		}
		ofy.async().put(ipAddress);
	}
	
	public IpAddress get(String ip) {
		IpAddress ipAddress = new IpAddress();
		
		if (cache != null && cache.containsKey(prefix + ip)) {
			ipAddress = (IpAddress) cache.get(prefix + ip);
		} else {
			try {
				ipAddress = ofy.get(new Key<IpAddress>(IpAddress.class, ip));
				if (cache != null) {
					cache.put(prefix + ip, ipAddress);
				}
			} catch (Exception e) {
				ipAddress = null;
			}
		}
		return ipAddress;
	}
	
	public IpAddress add(String ip, List<String> links) {
		boolean isChange = false;
		IpAddress ipAddress = get(ip);
		if (ipAddress == null) {
			ipAddress = new IpAddress();
			ipAddress.setIp(ip);
		}
		
		List<String> domains = ipAddress.getDomains();
		for (int i = 0; i < links.size(); i++) {
			if (!domains.contains(links.get(i)) && domains.size() <= limit) {
				isChange = true;
				domains.add(links.get(i));
			}
		}
		
		ipAddress.setDomains(domains);
		
		if (isChange) {
			if (cache != null) {
				 cache.put(prefix + ip, ipAddress);
			}
			insert(ipAddress);
		}
		
		return ipAddress;
	}
	
	public IpAddress insert(String ip, String domain) {
		boolean isChange = false;
		IpAddress ipAddress = get(ip);
		if (ipAddress == null) {
			ipAddress = new IpAddress();
			ipAddress.setIp(ip);
		}
		
		List<String> domains = ipAddress.getDomains();
		if (!domains.contains(domain) && domains.size() <= limit) {
			isChange = true;
			domains.add(domain);
		}
		
		ipAddress.setDomains(domains);
		
		if (isChange) {
			if (cache != null) {
				 cache.put(prefix + ip, ipAddress);
			}
			insert(ipAddress);
		}
		
		return ipAddress;
	}
	
	public void delete(IpAddress ipAddress) {
		if (cache != null && cache.containsKey(prefix + ipAddress.getIp())) {
			cache.remove(prefix + ipAddress.getIp());
		}
		ofy.delete(ipAddress);
	}
}
